var $ = jQuery.noConflict();
function validateTip(element,css,tipString,status){
    element.css(css);
    element.html(tipString);
    element.attr("validateStatus",status);
}

$(document).ready(function(){
    var loginName = $("#loginName");
    var password = $("#password");
    var login = $("#login");

    var t1 = $("#t1");
    var t2 = $("#t2");

    loginName.bind("blur",function () {
        if(loginName.val()=="" || loginName.val()==null){
            validateTip(t1,{"color":"red"}, " *用户名不能为空",false);
        }else {
            validateTip(t1,{"color":"red"}, " ",true);
        }
    });

    password.bind("blur",function () {
        if(password.val() == "" || password.val()== null){
            validateTip(t2,{"color":"red"}," *密码不能为空",false);
        }else{
            validateTip(t2,{"color":"red"}, " ",true);
        }
    });


    login.bind("click",function(){
        if(t1.attr("validateStatus")!="true" ){
            loginName.blur();

        }else if(t2.attr("validateStatus")!="true"){
            password.blur();
        }else {
            var data = {
                loginName:loginName.val() ,
                password:password.val()
            };
            $.ajax({
                type : "POST",
                url  : "easybuy/user/login",
                data : data,
                dataType : "json",
                xhrFields : {
                    withCredentials : true
                },
                crossDomain:true,
                success : function(result){
                    console.log(result);
                    if(result.ylogin == "success"){
                        localStorage.setItem("user",result.userName);
                        localStorage.setItem("userId",result.userId);
                        window.location.href = "index.html";
                    }else if(result.nlogin == "failed"){
                        validateTip(t2,{"color":"red"}," *用户名或密码错误",false);
                    }
                },
                error : function (result) {
                    if(result.login = "failed"){
                        validateTip(t2,{"color":"red"}," *访问错误",false);
                    }
                }

            });

        }

    });

});